package com.ncmmall.application.business.distribution;

import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.distribution.DistributorLevel;
import com.ncmmall.domain.business.distribution.DistributorLevelApplyGoods;
import com.ncmmall.domain.business.distribution.DistributorLevelApplyGoodsRepository;
import com.ncmmall.domain.business.distribution.DistributorLevelRepository;
import com.ncmmall.domain.business.distribution.QDistributorLevel;
import com.ncmmall.domain.business.distribution.QDistributorLevelApplyGoods;
import com.ncmmall.domain.business.goods.Goods;
import com.ncmmall.domain.business.goods.QGoods;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Splitter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DistributorLevelApplication {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private DistributorLevelRepository distributorLevelRepository;

    @Autowired
    private DistributorLevelApplyGoodsRepository distributorLevelApplyGoodsRepository;

    /**
     * 分销商等级修改
     *
     * @param params 分销商参数
     */
    public void modify(DistributorLevel params) {

        Requires.hasLength(params.getImage(), "请上传等级图片");
        Requires.hasLength(params.getName(), "等级名称不能为空");
        Requires.isTrue(params.getFirstLevelScale() != null && params.getFirstLevelScale() > 0 && params.getFirstLevelScale() < 1, "分佣比例范围0~1");
        Requires.isTrue(params.getSecondLevelScale() != null && params.getSecondLevelScale() > 0 && params.getSecondLevelScale() < 1, "分佣比例范围0~1");
        Requires.isTrue(params.getApplyType() != null && (params.getApplyType().equals(1) || params.getApplyType().equals(2)), "请选择申请条件");
        if (params.getApplyType().equals(2)) {
            Requires.hasLength(params.getGoodsIds(), "请选择指定商品");
        }

        int level = params.getLevel();

        DistributorLevel distributorLevel = queryChannelService.findOne(QDistributorLevel.distributorLevel.level.eq(level), DistributorLevel.class);
        if (distributorLevel == null) {
            distributorLevel = new DistributorLevel();
            distributorLevel.setLevel(level);
        }
        distributorLevel.setImage(params.getImage());
        distributorLevel.setName(params.getName());
        distributorLevel.setFirstLevelScale(params.getFirstLevelScale());
        distributorLevel.setSecondLevelScale(params.getSecondLevelScale());
        distributorLevel.setApplyType(params.getApplyType());

        List<DistributorLevel> bigs = queryChannelService.findAll(QDistributorLevel.distributorLevel.level.gt(level), DistributorLevel.class);
        List<DistributorLevel> smalls = queryChannelService.findAll(QDistributorLevel.distributorLevel.level.lt(level), DistributorLevel.class);

        //下级分销商数量

        if (BooleanUtils.isTrue(params.getEnableSubDistributorNum())) {

            Requires.isTrue(params.getSubDistributorNum() != null && params.getSubDistributorNum() >= 0, "下级分销商数量不能为空");
            Requires.isTrue(params.getSubDistributorMaxNum() != null && params.getSubDistributorMaxNum() > params.getSubDistributorNum(), "下级分销商数量不正确");

            for (DistributorLevel big : bigs) {
                if (BooleanUtils.isTrue(big.getEnableSubDistributorNum())) {
                    if (big.getSubDistributorNum() != null) {
                        Requires.isTrue(big.getSubDistributorNum() >= params.getSubDistributorMaxNum(), "下级分销商数量不能大于更高级别的分销商");
                    }
                }
            }

            for (DistributorLevel small : smalls) {
                if (BooleanUtils.isTrue(small.getEnableSubDistributorNum())) {
                    if (small.getSubDistributorMaxNum() != null) {
                        Requires.isTrue(small.getSubDistributorMaxNum() <= params.getSubDistributorNum(), "下级分销商数量不能小于更低级别的分销商");
                    }
                }
            }

            distributorLevel.setEnableSubDistributorNum(true);
            distributorLevel.setSubDistributorNum(params.getSubDistributorNum());
            distributorLevel.setSubDistributorMaxNum(params.getSubDistributorMaxNum());

        } else {
            distributorLevel.setEnableSubDistributorNum(null);
            distributorLevel.setSubDistributorNum(null);
            distributorLevel.setSubDistributorMaxNum(null);
        }

        //子孙分销商数量
        if (BooleanUtils.isTrue(params.getEnableChildDistributorNum())) {

            Requires.isTrue(params.getChildDistributorNum() != null && params.getChildDistributorNum() >= 0, "子孙分销商数量不能为空");
            Requires.isTrue(params.getChildDistributorMaxNum() != null && params.getChildDistributorMaxNum() > params.getChildDistributorNum(), "子孙分销商数量不正确");

            for (DistributorLevel big : bigs) {
                if (BooleanUtils.isTrue(big.getEnableChildDistributorNum())) {
                    if (big.getChildDistributorNum() != null) {
                        Requires.isTrue(big.getChildDistributorNum() >= params.getChildDistributorMaxNum(), "子孙分销商数量不能大于更高级别的分销商");
                    }
                }
            }

            for (DistributorLevel small : smalls) {
                if (BooleanUtils.isTrue(small.getEnableChildDistributorNum())) {
                    if (small.getChildDistributorMaxNum() != null) {
                        Requires.isTrue(small.getChildDistributorMaxNum() <= params.getChildDistributorNum(), "下子孙分销商数量不能小于更低级别的分销商");
                    }
                }
            }

            distributorLevel.setEnableChildDistributorNum(true);
            distributorLevel.setChildDistributorNum(params.getChildDistributorNum());
            distributorLevel.setChildDistributorMaxNum(params.getChildDistributorMaxNum());

        } else {
            distributorLevel.setEnableChildDistributorNum(null);
            distributorLevel.setChildDistributorNum(null);
            distributorLevel.setChildDistributorMaxNum(null);
        }

        //下线会员数
        if (BooleanUtils.isTrue(params.getEnableSubMemberNum())) {

            Requires.isTrue(params.getSubMemberNum() != null && params.getSubMemberNum() >= 0, "下线会员数量不能为空");
            Requires.isTrue(params.getSubMemberMaxNum() != null && params.getSubMemberMaxNum() > params.getSubMemberNum(), "下线会员数量不正确");

            for (DistributorLevel big : bigs) {
                if (BooleanUtils.isTrue(big.getEnableSubMemberNum())) {
                    if (big.getSubMemberNum() != null) {
                        Requires.isTrue(big.getSubMemberNum() >= params.getSubMemberMaxNum(), "下线会员数量不能大于更高级别的分销商");
                    }
                }
            }

            for (DistributorLevel small : smalls) {
                if (BooleanUtils.isTrue(small.getEnableSubMemberNum())) {
                    if (small.getSubMemberMaxNum() != null) {
                        Requires.isTrue(small.getSubMemberMaxNum() <= params.getSubMemberNum(), "下线会员数量不能小于更低级别的分销商");
                    }
                }
            }

            distributorLevel.setEnableSubMemberNum(true);
            distributorLevel.setSubMemberNum(params.getSubMemberNum());
            distributorLevel.setSubMemberMaxNum(params.getSubMemberMaxNum());

        } else {
            distributorLevel.setEnableSubMemberNum(null);
            distributorLevel.setSubMemberNum(null);
            distributorLevel.setSubMemberMaxNum(null);
        }

        //下线会员订单数
        if (BooleanUtils.isTrue(params.getEnableSubMemberOrderNum())) {

            Requires.isTrue(params.getSubMemberOrderNum() != null && params.getSubMemberOrderNum() >= 0, "下线会员订单数量不能为空");
            Requires.isTrue(params.getSubMemberOrderMaxNum() != null && params.getSubMemberOrderMaxNum() > params.getSubMemberOrderNum(), "下线会员订单数量不正确");

            for (DistributorLevel big : bigs) {
                if (BooleanUtils.isTrue(big.getEnableSubMemberOrderNum())) {
                    if (big.getSubMemberOrderNum() != null) {
                        Requires.isTrue(big.getSubMemberOrderNum() >= params.getSubMemberOrderMaxNum(), "下线会员订单数量不能大于更高级别的分销商");
                    }
                }
            }

            for (DistributorLevel small : smalls) {
                if (BooleanUtils.isTrue(small.getEnableSubMemberOrderNum())) {
                    if (small.getSubMemberOrderMaxNum() != null) {
                        Requires.isTrue(small.getSubMemberOrderMaxNum() <= params.getSubMemberOrderNum(), "下线会员订单数量不能小于更低级别的分销商");
                    }
                }
            }

            distributorLevel.setEnableSubMemberOrderNum(true);
            distributorLevel.setSubMemberOrderNum(params.getSubMemberOrderNum());
            distributorLevel.setSubMemberOrderMaxNum(params.getSubMemberOrderMaxNum());

        } else {
            distributorLevel.setEnableSubMemberOrderNum(null);
            distributorLevel.setSubMemberOrderNum(null);
            distributorLevel.setSubMemberOrderMaxNum(null);
        }

        //下线会员订单金额
        if (BooleanUtils.isTrue(params.getEnableSubMemberOrderMoney())) {

            Requires.isTrue(params.getSubMemberOrderMoney() != null && params.getSubMemberOrderMoney() >= 0, "下线会员订单金额不能为空");
            Requires.isTrue(params.getSubMemberOrderMaxMoney() != null && params.getSubMemberOrderMaxMoney() > params.getSubMemberOrderMoney(), "下线会员订单金额不正确");

            for (DistributorLevel big : bigs) {
                if (BooleanUtils.isTrue(big.getEnableSubMemberOrderMoney())) {
                    if (big.getSubMemberOrderMoney() != null) {
                        Requires.isTrue(big.getSubMemberOrderMoney() >= params.getSubMemberOrderMaxMoney(), "下线会员订单金额不能大于更高级别的分销商");
                    }
                }
            }

            for (DistributorLevel small : smalls) {
                if (BooleanUtils.isTrue(small.getEnableSubMemberOrderMoney())) {
                    if (small.getSubMemberOrderMaxMoney() != null) {
                        Requires.isTrue(small.getSubMemberOrderMaxMoney() <= params.getSubMemberOrderMoney(), "下线会员订单金额不能小于更低级别的分销商");
                    }
                }
            }

            distributorLevel.setEnableSubMemberOrderMoney(true);
            distributorLevel.setSubMemberOrderMoney(params.getSubMemberOrderMoney());
            distributorLevel.setSubMemberOrderMaxMoney(params.getSubMemberOrderMaxMoney());

        } else {
            distributorLevel.setEnableSubMemberOrderMoney(null);
            distributorLevel.setSubMemberOrderMoney(null);
            distributorLevel.setSubMemberOrderMaxMoney(null);
        }

        //成交订单数
        if (BooleanUtils.isTrue(params.getEnableOrderNum())) {

            Requires.isTrue(params.getOrderNum() != null && params.getOrderNum() >= 0, "成交订单数量不能为空");
            Requires.isTrue(params.getOrderMaxNum() != null && params.getOrderMaxNum() > params.getOrderNum(), "成交订单数量不正确");

            for (DistributorLevel big : bigs) {
                if (BooleanUtils.isTrue(big.getEnableOrderNum())) {
                    if (big.getOrderNum() != null) {
                        Requires.isTrue(big.getOrderNum() >= params.getOrderMaxNum(), "成交订单数量不能大于更高级别的分销商");
                    }
                }
            }

            for (DistributorLevel small : smalls) {
                if (BooleanUtils.isTrue(small.getEnableOrderNum())) {
                    if (small.getOrderMaxNum() != null) {
                        Requires.isTrue(small.getOrderMaxNum() <= params.getOrderNum(), "成交订单数量不能小于更低级别的分销商");
                    }
                }
            }

            distributorLevel.setEnableOrderNum(true);
            distributorLevel.setOrderNum(params.getOrderNum());
            distributorLevel.setOrderMaxNum(params.getOrderMaxNum());

        } else {
            distributorLevel.setEnableOrderNum(null);
            distributorLevel.setOrderNum(null);
            distributorLevel.setOrderMaxNum(null);
        }

        //成交金额
        if (BooleanUtils.isTrue(params.getEnableOrderMoney())) {

            Requires.isTrue(params.getOrderMoney() != null && params.getOrderMoney() >= 0, "成交金额不能为空");
            Requires.isTrue(params.getOrderMaxMoney() != null && params.getOrderMaxMoney() > params.getOrderMoney(), "成交金额不正确");

            for (DistributorLevel big : bigs) {
                if (BooleanUtils.isTrue(big.getEnableOrderMoney())) {
                    if (big.getOrderMoney() != null) {
                        Requires.isTrue(big.getOrderMoney() >= params.getOrderMaxMoney(), "成交金额不能大于更高级别的分销商");
                    }
                }
            }

            for (DistributorLevel small : smalls) {
                if (BooleanUtils.isTrue(small.getEnableOrderMoney())) {
                    if (small.getOrderMaxMoney() != null) {
                        Requires.isTrue(small.getOrderMaxMoney() <= params.getOrderMoney(), "成交金额不能小于更低级别的分销商");
                    }
                }
            }

            distributorLevel.setEnableOrderMoney(true);
            distributorLevel.setOrderMoney(params.getOrderMoney());
            distributorLevel.setOrderMaxMoney(params.getOrderMaxMoney());

        } else {
            distributorLevel.setEnableOrderMoney(null);
            distributorLevel.setOrderMoney(null);
            distributorLevel.setOrderMaxMoney(null);
        }

        distributorLevelRepository.save(distributorLevel);

        if (params.getApplyType().equals(2)) {
            //删除旧数据 如果有
            List<DistributorLevelApplyGoods> olds = queryChannelService.findAll(QDistributorLevelApplyGoods.distributorLevelApplyGoods.distributorLevel.eq(distributorLevel), DistributorLevelApplyGoods.class);
            if (CollectionUtils.isNotEmpty(olds)) {
                distributorLevelApplyGoodsRepository.delete(olds);
            }

            //保存新商品
            List<Long> ids = Splitter.on(";").omitEmptyStrings().trimResults().splitToList(params.getGoodsIds()).stream().map(Long::valueOf).collect(Collectors.toList());
            List<Goods> goodsList = queryChannelService.findAll(QGoods.goods.id.in(ids), Goods.class);
            for (Goods goods : goodsList) {
                DistributorLevelApplyGoods applyGoods = new DistributorLevelApplyGoods();
                applyGoods.setGoods(goods);
                applyGoods.setDistributorLevel(distributorLevel);
                applyGoods.setCreateTime(new Timestamp(System.currentTimeMillis()));
                distributorLevelApplyGoodsRepository.save(applyGoods);
            }
        }
    }

    public void remove(Long id) {
        List<DistributorLevelApplyGoods> all = queryChannelService.findAll(QDistributorLevelApplyGoods.distributorLevelApplyGoods.distributorLevel.id.eq(id), DistributorLevelApplyGoods.class);
        distributorLevelApplyGoodsRepository.delete(all);
        distributorLevelRepository.delete(id);
    }
}
